/***************************************************
 ** @Desc : This file for 修改分账用户信息
 ** @Time : 2018.12.26 16:33 
 ** @Author : Joker
 ** @File : sys_config
 ** @Last Modified by : Joker
 ** @Last Modified time: 2018.12.26 16:33
 ** @Software: GoLand
****************************************************/
package controllers

import (
	"github.com/astaxie/beego"
	"strconv"
	"strings"
	"sub_account/models"
	"sub_account/utils"
)

type SysConfigAct struct {
	beego.Controller
}

//展示列表
// @router /index/list.py/ [get]
func (this *SysConfigAct) ShowList() {
	this.TplName = "list.html"
}

/*查询分页*/
// @router /index/list_page.py/?:params [get]
func (this *SysConfigAct) QueryAndListPage() {
	//分页参数
	page, _ := strconv.Atoi(this.GetString("page"))
	limit, _ := strconv.Atoi(this.GetString("limit"))
	//第一次获取的页数和显示条数都是0
	if page == 0 {
		page = 1
	}
	if limit == 0 {
		limit = 20
	}
	//查询参数
	in := make(map[string]interface{})
	trade_out := this.GetString("trade_out")
	in["trans_out"] = trade_out

	//数据获取
	list := sys_config.SelectConfigListPage(in, limit, (page-1)*limit)
	count := sys_config.SelectConfigPageCount(in)

	//数据回显
	out := make(map[string]interface{})
	out["count"] = count //分页数据
	out["limit"] = limit
	out["page"] = page
	out["root"] = list //显示数据

	this.Data["json"] = out
	this.ServeJSON()
	this.StopRun()
}

//展示添加
// @router /index/add_ui.py/ [get]
func (this *SysConfigAct) ShowAddUI() {
	this.TplName = "add.html"
}

//实现添加分账
// @router /index/add.py/?:params [post]
func (this *SysConfigAct) AddSubAccount() {
	//获取数据
	trans_out := strings.TrimSpace(this.GetString("trans_out"))
	trans_in := strings.TrimSpace(this.GetString("trans_in"))
	app_id := strings.TrimSpace(this.GetString("app_id"))

	code, msg := utils.FAILED_FLAG, utils.FAILED_STRING
	if strings.Compare(trans_out, trans_in) == 0 {
		msg = "分账PID与收账PID不能相同! "
	} else {
		//获取分账PID的秘钥对
		sysConfig := sys_config.SelectOneConfigByTransOut(trans_out)

		//实体赋值
		config := models.SysConfig{}
		config.TransOut = trans_out
		config.TransIn = trans_in
		config.AppId = app_id
		config.PrivateKey = sysConfig.PrivateKey
		config.PublicKey = sysConfig.PublicKey

		//插入数据
		code, _ = sys_config.InsertConfig(config)
	}
	if code > 0 {
		msg = utils.SUCCESS_STRING
	}

	this.Data["json"] = global.JsonFormat(code, "", msg, "")
	this.ServeJSON()
	this.StopRun()
}

//展示修改页面
// @router /index/modify_ui.py/?:params [get]
func (this *SysConfigAct) ShowModifyUI() {
	editId := this.Ctx.Input.Param(":params")
	config := sys_config.SelectOneConfig(editId)

	//防止用户直接通过URL修改配置信息
	this.SetSession("configId", editId)

	this.Data["conf"] = config
	this.TplName = "update.html"
}

//修改信息
// @router /index/modify.py/?:params [post]
func (this *SysConfigAct) ModifySystenConfig() {
	trans_out := this.GetString("trans_out")
	trans_in := this.GetString("trans_in")
	app_id := this.GetString("app_id")
	public_key := this.GetString("public_key")
	private_key := this.GetString("private_key")

	flag := utils.FAILED_FLAG
	confId := this.GetSession("configId")
	if confId != nil {
		config := sys_config.SelectOneConfig(confId.(string))

		config.TransOut = trans_out
		config.TransIn = trans_in
		config.AppId = app_id
		config.PublicKey = public_key
		config.PrivateKey = private_key
		config.EditTime = global.GetNowTime()
		flag = sys_config.UpdateSysConfig(config)
	}

	this.Data["json"] = global.GetDatabaseStatus(flag)
	this.ServeJSON()
	this.StopRun()
}

//删除
// @router /index/delete.py/?:params [get]
func (this *SysConfigAct) DeleteConfig() {
	delId := this.Ctx.Input.Param(":params")

	flag := sys_config.DeleteConfig(delId)

	this.Data["json"] = global.GetDatabaseStatus(flag)
	this.ServeJSON()
	this.StopRun()
}
